﻿using Future.Common.Attribute;
using Future.Common.Enum;
using Future.Common.Exception;
using Future.Common.Models;
using Future.IBLL;
using Future.IDAL;
using Future.IService;
using Future.Model.DtoModel;
using Future.Model.SysModel;

namespace Future.BLL
{
    /// <summary>
    /// 描述：字典信息相关业务逻辑
    /// 创建人： yangkai
    /// 创建时间：2022/11/21 17:59:17
    /// </summary>
    public class DictionaryInfoBLL : IDictionaryInfoBLL
    {
        private readonly IDictDataDAL _dictDataDAL;
        private readonly IDictTypeDAL _dictTypeDAL;
        private readonly ICacheService _cacheService;

        public DictionaryInfoBLL(IDictDataDAL dictDataDAL, IDictTypeDAL dictTypeDAL, ICacheService cacheService)
        {
            _dictDataDAL = dictDataDAL;
            _dictTypeDAL = dictTypeDAL;
            _cacheService = cacheService;
        }

        public bool AddDictData(DictDataModel dicData)
        {
            List<DictDataModel> dictDataModels = _dictDataDAL.GetList(x => x.DictType == dicData.DictType && (x.DictLabel == dicData.DictLabel || x.DictValue == dicData.DictValue));
            if (dictDataModels.Any())
            {
                throw new BusinessException("字典数据已经存在请勿重复添加");
            }
            RemoveDictTypeCache(dicData.DictType);
            dicData.Id = Guid.NewGuid();
            return _dictDataDAL.Insert(dicData);
        }

        public bool CreateDictType(DictTypeModel dictTypeModel)
        {
            List<DictTypeModel> existDictTypes = _dictTypeDAL.GetList(x => x.DictType == dictTypeModel.DictType || x.DictName == dictTypeModel.DictName);
            if (existDictTypes.Any())
            {
                throw new BusinessException("字典名称或字典类型已经存在");
            }

            dictTypeModel.Id = Guid.NewGuid();
            return _dictTypeDAL.Insert(dictTypeModel);
        }

        public bool DeleteDictDataList(List<Guid> ids)
        {
            List<DictDataModel> dictDatas = _dictDataDAL.GetList(x => ids.Contains(x.Id));
            foreach (var dictData in dictDatas)
            {
                RemoveDictTypeCache(dictData.DictType);
            }

            return _dictDataDAL.Delete(x => ids.Contains(x.Id));
        }

        [Require]
        public bool DeleteDictTypeList(List<Guid> ids)
        {
            List<DictTypeModel> dictTypeModels = _dictTypeDAL.GetList(x => ids.Contains(x.Id));
            if (dictTypeModels == null)
            {
                throw new BusinessException("要删除的数据不存在");
            }

            IEnumerable<string> dictTypes = dictTypeModels.Select(x => x.DictType);
            foreach (string dictType in dictTypes)
            {
                RemoveDictTypeCache(dictType);
            }

            _dictDataDAL.Delete(x => dictTypes.Contains(x.DictType));
            return _dictTypeDAL.Delete(x => ids.Contains(x.Id));
        }

        public async Task<PageModel<DictDataModel>> DictDataPageList(DictDataQueryDto dictDataQueryDto, PageParModel page)
        {
            PageModel<DictDataModel> result = await _dictDataDAL.SelectPageList(dictDataQueryDto, page);
            return result;
        }

        public DictDataModel GetDictDataById(Guid id)
        {
            return _dictDataDAL.Get(x => x.Id == id);
        }

        public List<DictDataModel> GetDictDataByType(string dictType)
        {
            List<DictDataModel> dictDataModels = new();
            DictTypeModel dictTypeModel = _dictTypeDAL.Get(x => x.DictType == dictType);
            if (dictTypeModel == null || dictTypeModel.Status == StatusEnum.Stop)
            {
                return dictDataModels;
            }

            dictDataModels = _dictDataDAL.GetList(x => x.DictType == dictType && x.Status);
            return dictDataModels;
        }

        public DictTypeModel GetDictTypeById(Guid dictId)
        {
            return _dictTypeDAL.Get(x => x.Id == dictId);
        }

        public List<DictTypeModel> GetDictTypeList()
        {
            return _dictTypeDAL.GetList(x => x.Status == StatusEnum.Normal);
        }

        public PageModel<DictTypeModel> SelctPageList(DictTypeQueryDto dictTypeQueryDto, PageParModel page)
        {
            Task<PageModel<DictTypeModel>> result = _dictTypeDAL.SelctPageList(dictTypeQueryDto, page);
            return result.Result;
        }

        public bool UpdateDictData(DictDataModel dicData)
        {
            List<DictDataModel> dictDataModels = _dictDataDAL.GetList(x => x.DictType == dicData.DictType && x.Id != dicData.Id && (x.DictLabel == dicData.DictLabel || x.DictValue == dicData.DictValue));
            if (dictDataModels.Any())
            {
                throw new BusinessException("字典数据已经存在请勿重复添加");
            }

            DictDataModel oldDictDataModel = _dictDataDAL.Get(x => x.Id == dicData.Id);
            if (oldDictDataModel == null)
            {
                throw new BusinessException("没有找到要更新的数据");
            }
            oldDictDataModel.CssClass = dicData.CssClass;
            oldDictDataModel.ListClass = dicData.ListClass;
            oldDictDataModel.Status = dicData.Status;
            oldDictDataModel.DictLabel = dicData.DictLabel;
            oldDictDataModel.DictValue = dicData.DictValue;
            oldDictDataModel.DictSort = dicData.DictSort;

            RemoveDictTypeCache(dicData.DictType);
            return _dictDataDAL.Update(dicData);
        }

        /// <summary>
        /// 删除缓存的字典类型数据
        /// </summary>
        /// <param name="key"></param>
        private void RemoveDictTypeCache(string key)
        {
            string cacheKey = ("DictType" + key).ToLower();
            _cacheService.Remove(cacheKey);
        }

        [Require]
        public bool UpdateDictType(DictTypeModel dictTypeModel)
        {
            List<DictTypeModel> existDictTypes = _dictTypeDAL.GetList(x => (x.DictType == dictTypeModel.DictType || x.DictName == dictTypeModel.DictName) && x.Id != dictTypeModel.Id);
            if (existDictTypes.Any())
            {
                throw new BusinessException("字典名称或字典类型已经存在");
            }

            DictTypeModel oldDictTypeModel = _dictTypeDAL.Get(x => x.Id == dictTypeModel.Id);
            if (oldDictTypeModel == null)
            {
                throw new BusinessException("没有找到要更新的数据");
            }

            List<DictDataModel> dictDataModels = _dictDataDAL.GetList(x => x.DictType == oldDictTypeModel.DictType);
            if (dictDataModels.Any())
            {
                dictDataModels.ForEach(x =>
                {
                    x.DictType = dictTypeModel.DictType;
                });
                _dictDataDAL.UpdateMany(dictDataModels);
            }

            oldDictTypeModel.DictType = dictTypeModel.DictType;
            oldDictTypeModel.DictName = dictTypeModel.DictName;
            oldDictTypeModel.Status = dictTypeModel.Status;
            oldDictTypeModel.Remark = dictTypeModel.Remark;
            RemoveDictTypeCache(dictTypeModel.DictType);
            return _dictTypeDAL.Update(dictTypeModel);
        }
    }
}
